<template>
  <div :class="{ loading: loading }" ref="mapChartRef" style="height: 300px; width: 300px"></div>
</template>

<script setup lang="ts">
import { ref, onMounted, onUnmounted } from "vue";
import * as echarts from "echarts";
import worldCh from "@/assets/json/worldCh.json";
import china from "@/assets/json/china.json";
import city from "@/assets/json/city.json";

import { useChinaMap } from "@/hooks/charts/useChina";

import { VLoad } from "element-plus";

const currentImageSrc = ref("/cy/imgDoc/R-C.jpg");
const loading = ref(true);
const { option } = useChinaMap();
const mapChartRef = ref<HTMLDivElement>();
const initChinaMap = ref();
// 地图初始化
const mapInit = () => {
  if (mapChartRef.value) {
    initChinaMap.value = echarts.init(mapChartRef.value);
    // 注册地图数据
    echarts.registerMap("china", china);
    // 设置图表选项
    initChinaMap.value?.setOption(option);
  }
};
// 在组件挂载时初始化地图
onMounted(() => {
  setTimeout(() => {
    mapInit();
    loading.value = false; // 隐藏加载动画
  }, 1000);
});
onUnmounted(() => {
  if (initChinaMap.value) {
    initChinaMap.value.dispose();
    initChinaMap.value = null;
  }
});
</script>
<style>
/* 地图容器的基本样式 */
div {
  position: relative; /* 确保伪元素相对于该容器定位 */
  /* background-color: #f0f0f0; 可选：背景颜色 */
}

/* 加载动画的样式 */
div.loading::before {
  content: "";
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  width: 50px;
  height: 50px;
  border: 5px solid rgba(0, 0, 0, 0.3);
  border-top-color: #007bff; /* 加载动画的颜色 */
  border-radius: 50%;
  animation: spin 1s linear infinite;
  z-index: 2; /* 确保加载动画在地图上方 */
}

/* 加载动画的关键帧 */
@keyframes spin {
  from {
    transform: translate(-50%, -50%) rotate(0deg);
  }
  to {
    transform: translate(-50%, -50%) rotate(360deg);
  }
}
</style>
